package kr.pe.johnkim.checkmileage.mobile.rest.controller;

import kr.pe.johnkim.checkmileage.mobile.domain.CheckMileageMemberMileageLog;
import kr.pe.johnkim.checkmileage.mobile.rest.service.CheckMileageMemberMileageLogService;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Check Mileage 사용자 마일리지 이력 Controller 클래스 이다.
 * 
 * @author : johnkim <cale9797@gmail.com / Bettersoft, Inc.>
 * @Date : 12. 7. 24
 */

@Controller
@Path("/checkMileageMemberMileageLogController")
public class CheckMileageMemberMileageLogController {

    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(CheckMileageMemberMileageLogController.class);

    @Autowired
    private CheckMileageMemberMileageLogService checkMileageMemberMileageLogService = null;

    /**
     * 사용자 마일리지 이력을 저장 한다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/registerLog")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public CheckMileageMemberMileageLog registerLog(InputStream is) throws Exception {
        logger.info("registerLog");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonLog = new JSONObject(line);
        JSONObject jsonLog2 = jsonLog.getJSONObject("checkMileageMemberMileageLog");

        CheckMileageMemberMileageLog parameterMemberMileageLog = new CheckMileageMemberMileageLog();

        parameterMemberMileageLog.setCheckMileageId(jsonLog2.getString("checkMileageId"));
        parameterMemberMileageLog.setMerchantId(jsonLog2.getString("merchantId"));
        parameterMemberMileageLog.setContent(jsonLog2.getString("content"));
        parameterMemberMileageLog.setMileage(jsonLog2.getString("mileage"));
        parameterMemberMileageLog.setActivateYn(jsonLog2.getString("activateYn"));
        parameterMemberMileageLog.setModifyDate(jsonLog2.getString("modifyDate"));
        parameterMemberMileageLog.setRegisterDate(jsonLog2.getString("registerDate"));
        parameterMemberMileageLog.setCheckMileageMileagesIdCheckMileageMileages(jsonLog2.getString("checkMileageMileagesIdCheckMileageMileages"));

        CheckMileageMemberMileageLog resultMemberMileageLog = new CheckMileageMemberMileageLog();

        try {
            checkMileageMemberMileageLogService.registerLog(parameterMemberMileageLog);
            resultMemberMileageLog.setResult("SUCCESS");
        } catch (Exception e) {
            resultMemberMileageLog.setResult("FAIL");
            logger.error("Fail to register log.");
        }

        return resultMemberMileageLog;
    }

    /**
     * 특정 업체에 대한 마일리지 이력을 가져 온다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/selectMemberMileageLogList")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public List<CheckMileageMemberMileageLog> selectMemberMileageLogList(InputStream is) throws Exception {
        logger.info("selectMemberMileageLogList");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonLog = new JSONObject(line);
        JSONObject jsonLog2 = jsonLog.getJSONObject("checkMileageMemberMileageLog");

        CheckMileageMemberMileageLog parameterMemberMileageLog = new CheckMileageMemberMileageLog();

        parameterMemberMileageLog.setActivateYn(jsonLog2.getString("activateYn"));
        parameterMemberMileageLog.setCheckMileageMileagesIdCheckMileageMileages(jsonLog2.getString("checkMileageMileagesIdCheckMileageMileages"));

        return checkMileageMemberMileageLogService.selectMemberMileageLogList(parameterMemberMileageLog);
    }
}
